package com.muyu.product.service.impl;

import java.util.List;

import com.muyu.common.core.exception.ServiceException;
import com.muyu.common.core.utils.ObjUtils;
import com.muyu.product.domain.AsAttributeGroup;
import com.muyu.product.service.AsAttributeGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.muyu.product.mapper.AttributeInfoMapper;
import com.muyu.product.domain.AttributeInfo;
import com.muyu.product.service.AttributeInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * 商品属性Service业务层处理
 *
 * @author DongZeLiang
 * @date 2024-02-27
 */
@Slf4j
@Service
public class AttributeInfoServiceImpl extends ServiceImpl<AttributeInfoMapper, AttributeInfo>  implements AttributeInfoService {

    @Autowired
    private AsAttributeGroupService asAttributeGroupService;

    /**
     * 查询商品属性列表
     *
     * @param attributeInfo 商品属性
     * @return 商品属性
     */
    @Override
    public List<AttributeInfo> list(AttributeInfo attributeInfo) {
        LambdaQueryWrapper<AttributeInfo> queryWrapper = new LambdaQueryWrapper<>();


        if (ObjUtils.notNull(attributeInfo.getName())){
            queryWrapper.like(AttributeInfo::getName, attributeInfo.getName());
        }

        if (ObjUtils.notNull(attributeInfo.getCode())){
            queryWrapper.like(AttributeInfo::getCode, attributeInfo.getCode());
        }

        return list(queryWrapper);
    }

    /**
     * 通过groupId查询属性集合
     *
     * @param groupId 属性组Id
     *
     * @return 属性集合
     */
    @Override
    public List<AttributeInfo> attributeListByGroupId (Long groupId) {
        if (groupId == null){
            throw new ServiceException("查询商品属性组，属性组ID不可为空");
        }
        LambdaQueryWrapper<AsAttributeGroup> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AsAttributeGroup::getGroupId, groupId);
        List<Long> list = asAttributeGroupService.list(queryWrapper).stream()
                .map(AsAttributeGroup::getAttributeId)
                .toList();
        if (list == null || list.isEmpty()){
            return null;
        }
        return this.listByIds(
                list
        );
    }
}
